﻿using Authentication.Model.Interface;
using Authentication.Model.Model;
using Common.Contract;
using Common.Contract.Authentication;
using Common.Contract.Authentication.Identifier;
using Common.Contract.Authentication.Request;
using Common.Service.Command;
using Common.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Authentication.ServiceCommand
{
    public class UpdateUserServiceCommand : ProviderServiceCommand<IUserProvider, UpdateUserRequest, ResponseRecord>
    {
        public UpdateUserServiceCommand(IUserProvider providers) : base(providers) { }
        protected override void Validate(UpdateUserRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.Record == null)
            {
                throw new ArgumentException("Record must be set", "request");
            }
            if (request.Record.UserName.IsNullOrWhiteSpace())
            {
                throw new ArgumentException("UserName must be set", "request");
            }
            if (request.Record.FirstName.IsNullOrWhiteSpace())
            {
                throw new ArgumentException("First name must be set", "request");
            }
            if (request.Record.Email.IsNullOrWhiteSpace())
            {
                throw new ArgumentException("Email must be set", "request");
            }
        }

        protected override bool OnExecute(ResponseRecord response)
        {
            var applicationKey = Request.Header.ApplicationKey;
            var UserInfor = new AccountInfo
            {
                ApplicationKey = applicationKey.ToString().ToUpper(),
                UserName = Request.Record.UserName,
                Email = Request.Record.Email,
                FirstName = Request.Record.FirstName,
                LastName = Request.Record.LastName,
                MobilePin = Request.Record.Mobile,
                ImageUrl = Request.Record.ImageUrl,
                Birthday = Request.Record.Birthday,
                Gender = Request.Record.Gender,
                Address = Request.Record.Address,
                CanContactByEmail = BooleanString.FALSE,
                CreateUtcDate = DateTime.Now
            };

            var User = new Account
            {
                Active = Request.Record.Active.ToBooleanString(),
                ApplicationKey = applicationKey.ToString(),
                UserName = Request.Record.UserName,
                GroupId = Request.Record.GroupIdentifier,
                UpdateDate = DateTime.Now,
                AccountInfo = UserInfor
            };


            try
            {
                var savedUser = Providers.UpdateUser(User);
                response.IsSuccess = savedUser;
            }
            catch (Exception ex)
            {
                response.ErrorMessage = ex.Message;
                response.IsSuccess = false;
            }
            return response.IsSuccess;
        }
    }
}
